package com.lanqiao.controller;

import com.github.pagehelper.Page;
import com.lanqiao.service.IHouseService;
import com.lanqiao.vo.*;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Created by Administrator on 2018/10/4.
 */
@Controller
@RequestMapping("/house")
public class HouseController {

    @Resource
    IHouseService houseService;

    @RequestMapping("/search")
    public String search(House house, Integer pageNum, ModelMap map, HttpSession session){
        if (pageNum == null) {
            pageNum = 1;
            session.setAttribute("selective",house);
        }else{
            house = (House)session.getAttribute("selective");
        }
        Page<House> page = (Page<House>)houseService.selectBySelective(house, pageNum, 5);
        map.addAttribute("page",page);
        return "list";
    }

    @RequestMapping(value = "/load_district",method = RequestMethod.GET)
    @ResponseBody
    public List<District> loadDistrict(){
        return this.houseService.selectDistrict();
    }

    @RequestMapping(value = "/load_street_by_districtId",method = RequestMethod.GET)
    @ResponseBody
    public List<Street> loadStreetByDistrictId(@RequestParam("id") Integer districtId){
        return this.houseService.selectStreetByDistrictId(districtId);
    }

    @RequestMapping(value = "/load_types",method = RequestMethod.GET)
    @ResponseBody
    public List<Types> loadTypes(){
        return this.houseService.selectTypes();
    }

    @RequestMapping("/insert")
    public String insert(House house) {
        int i = this.houseService.insert(house);
        return (i==1 ? "redirect:/house/search" : "redirect:/views/post.jsp");
    }

    @RequestMapping("/find_for_me")
    public String selectByMe(Integer pageNum,ModelMap map,HttpSession session){
        UsersInfo user = (UsersInfo)session.getAttribute("logUser");
        if (user == null){
            return "redirect:/login_form.jsp";
        }
        if (pageNum == null) {
            pageNum = 1;
        }
        Page<House> page = (Page<House>) this.houseService.selectByMe(user.getUserId(),pageNum,5);
        map.addAttribute("page",page);
        return "my";
    }

    @RequestMapping("/findById/{id}") // .../house/findById/2/test/example?username=james&age=33
    public String findById(@PathVariable("id") Integer id, ModelMap modelMap) {
        modelMap.addAttribute("h", this.houseService.selectById(id));
        return "detail";
    }

    @RequestMapping("/toUpdate/{id}") // .../house/findById/2/test/example?username=james&age=33
    public String toUpdate(@PathVariable("id") Integer hid, ModelMap modelMap) {
        modelMap.addAttribute("house", this.houseService.selectById(hid));
        return "update_form";
    }

    @RequestMapping("/update")
    public String update(House house){
        int i = this.houseService.update(house);
        return (i==1 ? "redirect:/house/find_for_me?userId="+ house.getUserId() : "redirect:/house/findById/"+house.getId());
    }

    @RequestMapping("/deleteById/{id}")
    public String deleteById(@PathVariable("id") Integer id,HttpSession session){
        int i = this.houseService.delete(id);
        UsersInfo user = (UsersInfo)session.getAttribute("logUser");
        return "redirect:/house/find_for_me?userId="+ user.getUserId();
    }

}
